export default {
    xor_enc: (str, key = '25879') => {
        let decodeStr = '';
        let k, keyLen = key.length;
        for (let i = 0; i < str.length; i++) {
            k = i % keyLen;
            decodeStr += String.fromCharCode(str.charCodeAt(i) ^ key.charCodeAt(k));
        }
        return decodeStr;
    },

    /**
     * 图片解密(3合一)
     * @param srcCode 密文
     * @param vid 视频id
     * @returns {Promise<unknown>}{id:1,src:string}
     */
    imgDecode(srcCode, vid) {
        return new Promise((resolve, reject) => {
            let xmlHttp = new XMLHttpRequest();
            xmlHttp.open("GET", srcCode + '_.jpg', true);
            xmlHttp.responseType = "blob";
            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState !== 4 || xmlHttp.status !== 200) return;
                let reader = new FileReader();
                reader.readAsArrayBuffer(xmlHttp.response);
                reader.onload = function () {
                    let result = new Blob([this.result.slice(4)], {type: 'image/jpeg'});
                    let reader = new FileReader();
                    reader.readAsDataURL(result);
                    reader.onload = function (e) {
                        resolve({id: vid, src: e.target.result});
                    }
                };
            };
            xmlHttp.send(null);
        });
    },
};
